{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "88f0d172",
   "metadata": {},
   "source": [
    "# Creates \"green\" image using red and blue filters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c8fb2a0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from astropy.io import fits\n",
    "from astropy.wcs import WCS\n",
    "import glob, os\n",
    "import pandas as pd\n",
    "\n",
    "main_dir = '/Volumes/AGEL/HST_data'\n",
    "\n",
    "force = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8446439d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No F140W file for AGEL014504-045551A, skipping...\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "for target_dir in [d for d in glob.glob(os.path.join(main_dir, '*')) if os.path.isdir(d)]:\n",
    "    target = target_dir.split('/')[-1]\n",
    "    if target[-1] == 'B': \n",
    "        continue\n",
    "    \n",
    "    try:\n",
    "        fits_file1=glob.glob(f'{target_dir}/HST/16773_F140W/*drz_sci.fits')[0]\n",
    "    except:\n",
    "        try:\n",
    "            fits_file1=glob.glob(f'{target_dir}/HST/15867_F140W/*drz_sci.fits')[0]\n",
    "        except:\n",
    "            print(f'No F140W file for {target}, skipping...')\n",
    "            continue\n",
    "    try:\n",
    "        fits_file2=glob.glob(f'{target_dir}/HST/16773_F200LP/*_scaled_L3.fits')[0]\n",
    "        outfile = f'{target_dir}/HST/16773_F200LP/{target}_F200LP_WFC3_green_img_scaled_L3.fits'\n",
    "    except:\n",
    "        print(f'No F200LP scaled file for {target}, Looking for F606W...')\n",
    "        # continue\n",
    "        try:\n",
    "            fits_file2=glob.glob(f'{target_dir}/HST/17307_F606W/*_scaled_L3.fits')[0]\n",
    "            outfile = f'{target_dir}/HST/17307_F606W/{target}_F606W_ACS_green_img_scaled_L3.fits'\n",
    "        except:\n",
    "            print(f'No F606W scaled file for {target}, skipping...')\n",
    "            continue\n",
    "\n",
    "    file1=fits.open(fits_file1)\n",
    "    file2=fits.open(fits_file2)\n",
    "\n",
    "    dataf1=file1[0].data\n",
    "    dataf2=file2[0].data\n",
    "\n",
    "    green_image= np.sqrt(dataf1**2 + dataf2**2)\n",
    "    # green_image = (dataf1 + dataf2)/2\n",
    "\n",
    "    hdu=fits.PrimaryHDU(green_image)\n",
    "\n",
    "    wcs = WCS(file1[0].header)\n",
    "    hdu.header.update(wcs.to_header())\n",
    "\n",
    "    hdul_out = fits.HDUList([hdu])\n",
    "    hdul_out.writeto(outfile, overwrite=True)\n",
    "    print(f'green image written out for {target}')\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a0e8106",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "stenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
